import numpy as np
from .base import BaseTransformer

class OneHotEncoder(BaseTransformer):
    """
    OneHotEncoder 类将类别特征转换为独热编码（One-Hot Encoding）格式。
    """
    def __init__(self):
        super().__init__()
        self.classes_ = None

    def fit(self, X, y=None):
        """
        计算类别的所有唯一值
        """
        self.classes_ = np.unique(X)

    def transform(self, X):
        """
        将数据转换为独热编码格式
        """
        X = np.array(X)
        encoded = np.zeros((X.shape[0], len(self.classes_)))
        for i, val in enumerate(X):
            encoded[i, np.where(self.classes_ == val)[0]] = 1
        return encoded


class LabelEncoder(BaseTransformer):
    """
    LabelEncoder 类将类别特征转换为整数标签。
    """
    def __init__(self):
        super().__init__()
        self.class_map_ = None

    def fit(self, X, y=None):
        """
        创建类别到整数的映射
        """
        self.class_map_ = {val: idx for idx, val in enumerate(np.unique(X))}

    def transform(self, X):
        """
        将每个类别映射为对应的整数
        """
        return np.array([self.class_map_[val] for val in X])